home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8181 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Type casting
  5. Date: 1 Mar 1996 13:02:21 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4h7oktINN433@anvil.ugrad.cs.ubc.ca>
  8. References: <4gfnmi$gsc@calvin.risq.qc.ca> <4h5amm$dpv@umbc9.umbc.edu>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <4h5amm$dpv@umbc9.umbc.edu>,
  12. Jonas J. Schlein <schlein@umbc.edu> wrote:
  13.  >Pierre Coulombe <pcoulomb@criq.qc.ca> wrote:
  14.  >|> I have a problem with type casting in Visual C 1.5.
  15.  >|> I expected the following program to print the value 254 for valI.
  16.  >|> Instead it gives the output shown below.
  17.  >|> Can someone tell me where is my mistake ?
  18.  >|> 
  19.  >|> 
  20.  >|> ******** Program *********
  21.  >|> 
  22.  >|> #include <stdio.h>
  23.  >|> #include <string.h>
  24.  >|> #include <stdlib.h>
  25.  >|> 
  26.  >|> typedef unsigned int UINT;
  27.  >|> 
  28.  >|> #define MM_TO_UNITS 10.0F
  29.  >|> 
  30.  >|> void main(void)
  31.  >
  32.  >That's a no-no on c.l.c...Please read the FAQ and then you will fully
  33.  >understand why for your program a correct definition is 'int main (void)'.
  34.  
  35. The reason is a few sentences early in the ISO standard which say that main()
  36. shall be declared such and such, as I was surprised to find!
  37.  
  38. Any further justifications may be informative, but are actually superfluous. If
  39. the assertion were removed from the standard, it would be legal to declare
  40. main() any way you like, and the conforming environment would have to make sure
  41. to call it with a compatible sequence no matter how you define this. This _can_
  42. be done---for instance, the compiler, upon seeing a definition of main(), could
  43. insert a hidden function which can be called by the environment in a consistent
  44. manner, and which calls main() using a compatible calling sequence tailored to
  45. the definition of main(). Thus arguments about things like calling conventions
  46. are irrelevant. The standard only requires that function calls _within_ the
  47. program are compatible with the matching function definitions. However, main()
  48. is not called from an expression within the C program, so this rule doesn't
  49. apply to it.
  50.  
  51. You are required to write main() a certain way if you expect to write standard
  52. C, and that is all there is to it. There is no necessary reason why it _has_ to
  53. be that way.
  54.  
  55. Incidentally, the only specific reference to undefined behavior in the context
  56. of main() that I have been able to dig up in the Standard says that the exit
  57. status of a program which doesn't provide a return value on exit from main() is
  58. undefined. There is no discussion over T main() where T is not int, versus int
  59. main().
  60. -- 
  61.  
  62.